<template>
  <!-- 新增 -->
  <div class="add">
    <el-dialog
      class="title"
      :title="{ add: '新增用户', editor: '编辑用户' }[mode]"
      :visible.sync="isShow"
      width="477"
      center
      @close="closeEvent"
      @open="openEvent"
    >
      <el-form label-width="80px" :model="form" :rules="rules" ref="form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择角色">
            <el-option label="超级管理员" value="1"></el-option>
            <el-option label="管理员" value="2"></el-option>
            <el-option label="老师" value="3"></el-option>
            <el-option label="学生" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="禁用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit } from '@/api/user.js'
export default {
  data () {
    return {
      userInfo: {},
      mode: 'add',
      isShow: false,
      form: {
        username: '',
        email: '',
        phone: '',
        role: '',
        role_id: '',
        status: '',
        remark: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' },
          {
            min: 2,
            max: 6,
            message: '请输入2-6位的用户名',
            trigger: 'change'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const rag = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
              if (rag.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入邮箱'))
              }
            }
          }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const rag =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (rag.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            }
          }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }]
      }
    }
  },
  methods: {
    cancelEvent () {
      this.isShow = false
    },
    // 确定按钮
    submit () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === 'add') {
            await userAdd(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'editor') {
            await userEdit(this.form)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          this.$emit('getData')
        }
      })
    },
    // 重置表单验证与数据
    closeEvent () {
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        email: '',
        phone: '',
        role: '',
        status: '',
        remark: ''
      }
    },

    // 打开弹框执行
    openEvent () {
      if (this.mode === 'editor') {
        const userInfo = JSON.parse(JSON.stringify(this.userInfo))
        this.form = { ...userInfo }
        this.form.role_id = '' + this.form.role_id
        // console.log(this.form)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.add {
  /deep/span.el-dialog__title {
    color: #fff;
  }
  /deep/.el-dialog__header {
    background: linear-gradient(to right, #01c5fa, #1394fa);
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
